package com.shuang.solrdao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Repository;

import com.shuang.pojo.SolrGoodsPojo;
import com.shuang.solr.SolrSearchMapper;
@Repository("solrSearchMapper")
public class SolrSearchMapperImpl implements SolrSearchMapper {

	
	@Resource(name="solrServer")
	private SolrServer solrServer;
	@Override
	public Map<String, Object> searchGoods(SolrQuery queryString) throws SolrServerException {
		Map<String, Object> map=new HashMap<String, Object>();
		//查询索引库
		QueryResponse response = solrServer.query(queryString);
		//查询结果
		SolrDocumentList result = response.getResults();
		//总页数
		long total=result.getNumFound();
		map.put("total", total);
		//高亮区域获取
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		List<SolrGoodsPojo> list=new ArrayList<SolrGoodsPojo>();
		for(SolrDocument re:result){//只需要查到这些就可以显示了
			SolrGoodsPojo solrGoodsPojo=new SolrGoodsPojo();
			solrGoodsPojo.setId(Long.parseLong((String)re.get("id")));
			//取高亮显示的结果
			List<String> highList = highlighting.get(re.get("id")).get("g_title");
			String title="";
			if(highList!=null&&highList.size()>0){
				title=highList.get(0);
			}else{
				title=(String)re.get("g_title");
			}
			solrGoodsPojo.setG_title(title);
			solrGoodsPojo.setG_main_image((String)re.get("g_main_image"));
			solrGoodsPojo.setG_price((Long)re.get("g_price"));
			list.add(solrGoodsPojo);
		}
		map.put("data", list);
		return map;
	}

}
